package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.DiscountCoupon;
import com.lutzyt.delivery.bean.DiscountCouponItem;
import com.lutzyt.delivery.service.DiscountCouponItemService;
import com.lutzyt.delivery.service.DiscountCouponService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-06-01
 */
@Slf4j
@RestController
@RequestMapping("/api/discountCoupon")
public class DiscountCouponController extends ActionAuditController<DiscountCouponService, DiscountCouponService, DiscountCoupon> {
  private DiscountCouponService service;
  private DiscountCouponItemService itemService;

  @GetMapping("/{id}")
  public Result<DiscountCoupon> getById(@PathVariable Integer id) {
    final DiscountCoupon bean = service.getById(id);
    return Result.success(bean);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody DiscountCoupon bean) {
    log.info("更新优惠券:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<IPage<DiscountCoupon>> page(Integer pageSize, Integer pageNum) {
    final IPage<DiscountCoupon> page = service.findByPage(pageNum, pageSize);
    return Result.success(page);
  }

  @PostMapping("/draw/list")
  public Result<List<DiscountCoupon>> list() {
    final List<DiscountCoupon> page = service.drawList();
    return Result.success(page);
  }

  @PostMapping("/item/page")
  public Result<IPage<DiscountCouponItem>> itemPage(Integer pageSize, Integer pageNum, Integer id) {
    final IPage<DiscountCouponItem> page = itemService.findByPage(pageNum, pageSize,id);
    return Result.success(page);
  }

  @PostMapping("/item/draw")
  public Result<Void> draw(Integer customerId, Integer discountCouponId){
    itemService.draw(customerId, discountCouponId);
    return Result.success();
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("pl删除:{}", ids);
    service.deleteByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody DiscountCoupon bean) {
    log.info("添加优惠券:{}", bean);
    bean.setBalance(bean.getNum());
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setItemService(DiscountCouponItemService itemService) {
    this.itemService = itemService;
  }

  @Autowired
  public void setService(DiscountCouponService service) {
    this.service = service;
  }
}
    